'use strict';

angular.module('app')
    .controller('categoryController', ['$scope', '$rootScope', '$state', '$timeout', 'toaster',
        function ($scope, $rootScope, $state, $timeout, toaster) {
            $scope.title = '设备类型列表';
            $scope.param = {};
            $scope.loading = false;

            $scope.search = function () {
                $scope.loading = true;
                if ($scope.pageFlag != 1) {//未点击翻页
                    $scope.param.pageNum = 1;//将页码设置为1，即跳转回第一页
                } else {//点击翻页
                    $scope.pageFlag = 2;//将开关值设置为2
                }
                $.ajax({
                    type: 'POST',
                    dataType: 'json',
                    contentType: 'application/json;charset=UTF-8',
                    url: '/dac/dm/category/list',
                    data: angular.toJson($scope.param)
                }).then(function (result) {
                    console.log(result);
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }
            
            $scope.loadCategoryTypes = function () {
                $scope.param = {};
                $scope.param.type='DEVICE_CLASS';
                $.ajax({
                    type: 'PUT',
                    dataType: 'json',
                    contentType:'application/json;charset=UTF-8',
                    url : '/dic/queryDicListByType',
                    data: angular.toJson($scope.param)
                }).then(function(result) {
                    if (result.httpCode == 200) {
                        $scope.categoryTypes = result.data;
                        $scope.$apply();
                    } 
                    
                });
            }
            $scope.loadCategoryTypes();
            
            //从本地获取localstorage数据，对权限按钮做隐藏
            var personAuth = localStorage.getItem("PersonAuth");//获取PersonAuth的值
            personAuth = personAuth.split(',');
            for (var i = 0; i < personAuth.length; i++) {
                if (personAuth[i] == 'dm.deviceCategory.read') {
                    $scope.read = true;
                }
                if (personAuth[i] == 'dm.deviceCategory.add') {
                    $scope.add = true;
                }
                if (personAuth[i] == 'dm.deviceCategory.update') {
                    $scope.update = true;
                }
                if (personAuth[i] == 'dm.deviceCategory.delete') {
                    $scope.dele = true;
                }
            }
            $scope.search();

            $scope.clearSearch = function () {
                $scope.param.keyword = null;
                $scope.search();
            }

            $scope.disableItem = function (id, enable) {

            }

            $scope.delCategory = function (categoryName,categoryId) {
                if (confirm('确定要删除设备类型' + '[' + categoryName + ']' + '吗?'+'此操作会删除相关的设备列表、设备能力信息！')) {
                    $scope.delParam = {};
                    $scope.delParam.categoryId = categoryId;
                    $scope.loading = true;
                    $.ajax({
                        type: 'POST',
                        dataType: 'json',
                        contentType: 'application/json;charset=UTF-8',
                        url: '/dac/dm/category/delete',
                        data: angular.toJson($scope.delParam)
                    }).then(function (result) {
                        if (result.httpCode == 200) {
                            $scope.loading = false;
                            $scope.search();
                        } else {
                            toaster.clear('*');
                            toaster.pop('error', '', result.msg);
                        }
                    });
                }
            }

            // 翻页
            $scope.pagination = function (page) {
                $scope.param.pageNum = page;
                $scope.pageFlag = 1;//将开关值设置为1
                $scope.search();
            };

            $scope.deviceOpen = function (item) {

                var datas = {
                    msg: '7EFFFFFFFF46432D38393130413137303430313032FFFFFFFF1901000000000D00005AF89B1706191743210101177E',
                    devId: '7664189',
                    devKey: 'MWTCUzhFutIoeJEZO9jey88p5dw='
                };

//        	$scope.loading = true;
                $.ajax({
                    type: 'POST',
                    dataType: 'json',
                    contentType: 'application/json;charset=UTF-8',
                    url: '/hwapi/hw/api',
                    data: datas
                }).then(function (result) {
//		        $scope.loading = false;
                    if (result.httpCode == 200) {
//					$scope.pageInfo = result;
                        //toaster.clear('*');
                        toaster.pop('success', '', "请求成功");
                    } else {
//					$scope.msg = result.msg;
                        toaster.clear('*');
                        toaster.pop('success', '', "请求成功");
                    }
//				$scope.$apply();
                });
            }

        }]);